package com.guaju.yueshangLife.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.res.TypedArray;
import android.databinding.DataBindingUtil;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.adapter.OrderListAdapter;
import com.guaju.yueshangLife.databinding.ActivityDialogCancleOrderReasonBinding;
import com.guaju.yueshangLife.databinding.ActivityDialogReasonBinding;

public class CancleOrderReasonDialogActivity extends Activity implements View.OnClickListener {
    private static final String TAG = "GUAJU";
    private static final String LIST = "reason";
    //定义Activity退出动画的成员变量
    protected int activityCloseEnterAnimation;
    protected int activityCloseExitAnimation;
    ActivityDialogCancleOrderReasonBinding binding;
    RadioButton[] radioButtons;
    public static String[] reasons = {"订单不能按预计时间送达", "操作有误（商品、地址等信息）", "重复下单/误下单", "不想买了", "商品无货了", "其他原因"};
    private int selectedReason = -1;
    private int orderId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_dialog_cancle_order_reason);
        protectAnim();
        setListener();
        initViews();
    }

    private void initViews() {
        int offset = getResources().getDimensionPixelOffset(R.dimen.dp_16);
        radioButtons = new RadioButton[]{binding.rb1, binding.rb2, binding.rb3, binding.rb4, binding.rb5, binding.rb6};
        for (int i = 0; i < radioButtons.length; i++) {
            Drawable drawable = getResources().getDrawable(R.drawable.selector_refund_reason);
            //获取drawables
            Rect r = new Rect(0, 0, offset, offset);
            //定义一个Rect边界
            drawable.setBounds(r);
            radioButtons[i].setCompoundDrawablesRelative(null, null, drawable, null);
            radioButtons[i].setTextAppearance(CancleOrderReasonDialogActivity.this, R.style.reason_radio);
//            radioButtons[i].setText(reasons[i]);
        }
        Intent intent = getIntent();
        int selected = intent.getIntExtra(OrderListAdapter.REQUEST_REASON_SELECTED, -1);
        if (selected >= 0 && selected < reasons.length) {
            radioButtons[selected].setChecked(true);
        }
        orderId = intent.getIntExtra(OrderListAdapter.ORDER_ID, -1);
    }

    private void setListener() {
        binding.close.setOnClickListener(this);
        binding.radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb1:
                        selectedReason = 0;
                        break;
                    case R.id.rb2:
                        selectedReason = 1;
                        break;
                    case R.id.rb3:
                        selectedReason = 2;
                        break;
                    case R.id.rb4:
                        selectedReason = 3;
                        break;
                    case R.id.rb5:
                        selectedReason = 4;
                        break;
                    case R.id.rb6:
                        selectedReason = 5;
                        break;
                }
            }
        });
        binding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(OrderListAdapter.REQUEST_REASON, reasons[selectedReason]);
                intent.putExtra(OrderListAdapter.REQUEST_REASON_SELECTED, selectedReason);
                intent.putExtra(OrderListAdapter.ORDER_ID,orderId);
                setResult(RefundRequestActivity.RESULT_SELECT_REASON, intent);
                finish();
            }
        });
    }


    //    解决退出SecondActivity从底部退出动画失效问题
    private void protectAnim() {
        TypedArray activityStyle = getTheme().obtainStyledAttributes(new int[]{android.R.attr.windowAnimationStyle});
        int windowAnimationStyleResId = activityStyle.getResourceId(0, 0);
        activityStyle.recycle();
        setFinishOnTouchOutside(true);//点击空白处，窗体不关闭
        activityStyle = getTheme().obtainStyledAttributes(windowAnimationStyleResId, new int[]{android.R.attr.activityCloseEnterAnimation, android.R.attr.activityCloseExitAnimation});
        activityCloseEnterAnimation = activityStyle.getResourceId(0, 0);
        activityCloseExitAnimation = activityStyle.getResourceId(1, 0);
        activityStyle.recycle();

        //设置布局在底部
        getWindow().setGravity(Gravity.BOTTOM);
        //设置布局填充满宽度
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(layoutParams);
    }

    //    重写finish方法
    @Override
    public void finish() {
        super.finish();
        //finish时调用退出动画
        overridePendingTransition(activityCloseEnterAnimation, activityCloseExitAnimation);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;

            default:
                break;
        }
    }
}